package com.neiquan.backstage.resourec_manage.webaction;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Paging;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.common.util.Strings;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.Entrepreneur;
import com.neiquan.backstage.resourec_manage.EntrepreneurService;

@Controller
@RequestMapping(value = "allianceEntrepreneur", produces = { "text/json;charset=UTF-8" })
public class EntrepreneurAction {

	private static Class<?> poc = Entrepreneur.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private EntrepreneurService entrepreneurService;
	
	/**
	 * 返回全部Entrepreneur已审核成功的
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "allEntrepreneurStatus1")
	public String allEntrepreneurStatus1(HttpSession session)  {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Entrepreneur> list=(List<Entrepreneur>) extraSpringHibernateTemplate.getHibernateTemplate().find("From Entrepreneur where status=? and memberId=?", 1,memberId);
		return CommonResp.buildSuccessResp(list).toJsonString();
	}

	/**
	 * 添加Entrepreneur
	 */
	@ResponseBody
	@RequestMapping(value = { "addEntrepreneur" })
	public String addStation(String name, String loge, String description, String linkman,
			String phone, String mail, String address, HttpSession session,
			String projectType, String projectDesc, Integer teamSize) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		if (entrepreneurService.queryByName(Strings.trimAndNotNull(name),memberId) != null) {
			return CommonResp.buildErrorResp(BackendErrorCode.already_exist).toJsonString();
		}
		Entrepreneur entity = new Entrepreneur();
		entity.setName(Strings.trimAndNotNull(name));
		entity.setLoge(Strings.trimAndNotNull(loge));
		entity.setDescription(Strings.trimAndNotNull(description));
		entity.setLinkman(Strings.trimAndNotNull(linkman));
		entity.setPhone(Strings.trimAndNotNull(phone));
		entity.setMail(Strings.trimAndNotNull(mail));
		entity.setAddress(Strings.trimAndNotNull(address));
		entity.setProjectType(Strings.trimAndNotNull(projectType));
		entity.setProjectDesc(Strings.trimAndNotNull(projectDesc));
		entity.setTeamSize(teamSize);
		entity.setSaveTime(System.currentTimeMillis());
		entity.setStatus(0);
		entity.setMemberId(memberId);
		entrepreneurService.add(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	// 分页查询
	@ResponseBody
	@RequestMapping(value = { "list" })
	public String list(int pageIndex, int pageSize,HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<?> find =  extraSpringHibernateTemplate.getHibernateTemplate().find("From Entrepreneur where memberId=?", memberId);
		return CommonResp.buildSuccessResp(find).toJsonString();
	}

	// 审核通过
	@ResponseBody
	@RequestMapping(value = { "succeed" })
	public String congelation(int id) {
		Entrepreneur entity = entrepreneurService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		entity.setStatus(1);
		entrepreneurService.updata(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	// 审核失败
	@ResponseBody
	@RequestMapping(value = { "failure" })
	public String congelationAll(int id) {
		Entrepreneur entity = entrepreneurService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		entity.setStatus(2);
		entrepreneurService.updata(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	// 通过Id查询Entrepreneur
	@ResponseBody
	@RequestMapping(value = { "queryById" })
	public String queryById(int id) {
		Entrepreneur entity = entrepreneurService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		return CommonResp.buildSuccessResp(entity).toJsonString();
	}

	// 修改entrepreneur
	@ResponseBody
	@RequestMapping(value = { "edit" })
	public String edit(int id, String name, String loge, String description, String linkman, String phone, String mail, String address, String projectType, String projectDesc, Integer teamSize) {
		Entrepreneur entity = entrepreneurService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		entity.setName(Strings.trimAndNotNull(name));
		entity.setLoge(Strings.trimAndNotNull(loge));
		entity.setDescription(Strings.trimAndNotNull(description));
		entity.setLinkman(Strings.trimAndNotNull(linkman));
		entity.setPhone(Strings.trimAndNotNull(phone));
		entity.setMail(Strings.trimAndNotNull(mail));
		entity.setAddress(Strings.trimAndNotNull(address));
		entity.setProjectType(Strings.trimAndNotNull(projectType));
		entity.setProjectDesc(Strings.trimAndNotNull(projectDesc));
		entity.setTeamSize(teamSize);
		entrepreneurService.updata(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	// 条件查询
	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String linkman, String name,HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(linkman)) {
			criterions.add(Restrictions.like("linkman", "%" + linkman + "%"));
		}
		if (StringUtils.isNoneBlank(name)) {
			criterions.add(Restrictions.like("name", "%" + name + "%"));
		}
		criterions.add(Restrictions.eq("memberId", memberId));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("entrepreneurId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	// 显示全部
	@ResponseBody
	@RequestMapping(value = "allAuthstr")
	public String allAuthstr(int pageIndex, int pageSize,HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<?> find =  extraSpringHibernateTemplate.getHibernateTemplate().find("From Entrepreneur where memberId=?", memberId);
		return CommonResp.buildSuccessResp(find).toJsonString();
	}

	/**
	 * 返回全部Entrepreneur不带分页
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "allEntrepreneur")
	public String allEntrepreneur(HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<?> find =  extraSpringHibernateTemplate.getHibernateTemplate().find("From Entrepreneur where memberId=?", memberId);
		return CommonResp.buildSuccessResp(find).toJsonString();
	}

	// 显示待审核
	@ResponseBody
	@RequestMapping(value = "authstr")
	public String authstr(HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Criterion> criterions = new ArrayList<>();
		criterions.add(Restrictions.eq("status", 0));
		criterions.add(Restrictions.eq("memberId", memberId));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("entrepreneurId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	// 显示已审核成功
	@ResponseBody
	@RequestMapping(value = "successAudit")
	public String successAudit(HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Criterion> criterions = new ArrayList<>();
		criterions.add(Restrictions.eq("status", 1));
		criterions.add(Restrictions.eq("memberId", memberId));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("entrepreneurId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		System.out.println(CommonResp.buildSuccessResp(paging).toJsonString());
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	// 显示审核失败
	@ResponseBody
	@RequestMapping(value = "failureAudit")
	public String failureAudit(HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Criterion> criterions = new ArrayList<>();
		criterions.add(Restrictions.eq("status", 2));
		criterions.add(Restrictions.eq("memberId", memberId));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("entrepreneurId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		System.out.println(CommonResp.buildSuccessResp(paging).toJsonString());
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
}
